




/*
'===============================================================================
'  TuanHuy.BL.ProductsManagerBase
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using TuanHuy.BLL;
using TuanHuy.DAL;
using TuanHuy.DAL.EntityClasses;
using TuanHuy.DAL.FactoryClasses;
using TuanHuy.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace TuanHuy.BLL
{
	public class ProductsManagerBase : ManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		
		public ProductsManagerBase()
		{
			// Nothing for now.
		}
		
		/*public override string TableName()
        {
            return "Products";
        }*/
		
		public ProductsEntity Insert(ProductsEntity _ProductsEntity)
		{
			return Insert(_ProductsEntity, "");
		}
		
		public ProductsEntity Insert(ProductsEntity _ProductsEntity, string controlId)
		{
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.SaveEntity(_ProductsEntity, true);
				ProductsFields field = new ProductsFields();
                //LogEntity(_ProductsEntity, field, AcInsert, controlId);
			}
			return _ProductsEntity;
		}

		
		public ProductsEntity Insert(decimal ProductId, DateTime CreateDate)
		{
			ProductsEntity _ProductsEntity = new ProductsEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_ProductsEntity.ProductId = ProductId;
				_ProductsEntity.CreateDate = CreateDate;
				ProductsFields field = new ProductsFields();
                //LogEntity(_ProductsEntity, field, AcInsert);
				adapter.SaveEntity(_ProductsEntity, true);
			}
			return _ProductsEntity;
		}

		/*public ProductsEntity Insert(DateTime CreateDate)
		{
			ProductsEntity _ProductsEntity = new ProductsEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_ProductsEntity.CreateDate = CreateDate;
				adapter.SaveEntity(_ProductsEntity, true);
				ProductsFields field = new ProductsFields();
                //LogEntity(_ProductsEntity, field, AcInsert);
			}
			return _ProductsEntity;
		}*/
		
		public bool Update(ProductsEntity _ProductsEntity)
        {
            return Update(_ProductsEntity, "");
        }

		public bool Update(ProductsEntity _ProductsEntity, string controlId)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				RelationPredicateBucket filter = new RelationPredicateBucket();
				IPredicateExpression _PredicateExpression = new PredicateExpression();
				_PredicateExpression.Add(ProductsFields.ProductId == _ProductsEntity.ProductId);
					
				filter.PredicateExpression.Add(_PredicateExpression);

				adapter.UpdateEntitiesDirectly(_ProductsEntity, filter);
				toReturn = true;
				
				ProductsFields field = new ProductsFields();
                //LogEntity(_ProductsEntity, field, AcUpdate, controlId);
			}
			return toReturn;
		}
		
		public bool Update(ProductsEntity _ProductsEntity, RelationPredicateBucket filter)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.UpdateEntitiesDirectly(_ProductsEntity, filter);
				toReturn = true;
				
				ProductsFields field = new ProductsFields();
                //LogEntity(_ProductsEntity, field, AcUpdate);
			}
			return toReturn;
		}

		public bool Update(decimal ProductId, DateTime CreateDate)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				ProductsEntity _ProductsEntity = new ProductsEntity(ProductId);
				if (adapter.FetchEntity(_ProductsEntity))
				{
				
					_ProductsEntity.CreateDate = CreateDate;
					adapter.SaveEntity(_ProductsEntity, true);
					toReturn = true;
					
					ProductsFields field = new ProductsFields();
					//LogEntity(_ProductsEntity, field, AcUpdate);
				}
			}
			return toReturn;
		}

		public bool Delete(decimal ProductId)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				ProductsEntity _ProductsEntity = new ProductsEntity(ProductId);
				if (adapter.FetchEntity(_ProductsEntity))
				{
					adapter.DeleteEntity(_ProductsEntity);
					toReturn = true;
				}
			}
			return toReturn;
		}
		
		public void DeleteAll() 
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.DeleteEntitiesDirectly("ProductsEntity", null);
			}
		}
		
		
		public int DeleteByProductId(decimal ProductId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.ProductId == ProductId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("ProductsEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByCreateDate(DateTime CreateDate)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.CreateDate == CreateDate);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("ProductsEntity", filter);
			}
			return toReturn;
		}
		

		
		public ProductsEntity SelectOne(decimal ProductId)
		{
			ProductsEntity toReturn = null;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				ProductsEntity _ProductsEntity = new ProductsEntity(ProductId);
				if (adapter.FetchEntity(_ProductsEntity))
				{
					toReturn = _ProductsEntity;
				}
			}
			return toReturn;
		}

		// Return DataTable
		public DataTable SelectAllRDT()
		{
			DataTable toReturn = new DataTable();
			EntityCollection _ProductsCollection = new EntityCollection(new ProductsEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_ProductsCollection.EntityFactoryToUse.CreateFields(), toReturn, null, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectAllLST()
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, null, 0, null);
			}
			return _ProductsCollection;
		}
		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectAllLST_Paged(int PageNumber, int PageSize)
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, null, 0, null, null, PageNumber, PageSize);
			}
			return _ProductsCollection;
		}


		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectByProductIdLST(decimal ProductId)
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.ProductId == ProductId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, filter, 0, null);
			}
			return _ProductsCollection;
		}
		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectByProductIdLST_Paged(decimal ProductId, int PageNumber, int PageSize)
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.ProductId == ProductId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, filter, 0, null, null, PageNumber, PageSize);
			}
			return _ProductsCollection;
		}
		
		// Return DataTable
		public DataTable SelectByProductIdRDT(decimal ProductId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _ProductsCollection = new EntityCollection(new ProductsEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.ProductId == ProductId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_ProductsCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return DataTable
		public DataTable SelectByProductIdRDT_Paged(decimal ProductId, int PageNumber, int PageSize)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _ProductsCollection = new EntityCollection(new ProductsEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.ProductId == ProductId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_ProductsCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, 0, null, true, null, PageNumber, PageSize);
			}
			return toReturn;
		}


		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectByCreateDateLST(DateTime CreateDate)
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.CreateDate == CreateDate);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, filter, 0, null);
			}
			return _ProductsCollection;
		}
		
		// Return EntityCollection<ProductsEntity>
		public EntityCollection<ProductsEntity> SelectByCreateDateLST_Paged(DateTime CreateDate, int PageNumber, int PageSize)
		{
			EntityCollection<ProductsEntity> _ProductsCollection = new EntityCollection<ProductsEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.CreateDate == CreateDate);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_ProductsCollection, filter, 0, null, null, PageNumber, PageSize);
			}
			return _ProductsCollection;
		}
		
		// Return DataTable
		public DataTable SelectByCreateDateRDT(DateTime CreateDate)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _ProductsCollection = new EntityCollection(new ProductsEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.CreateDate == CreateDate);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_ProductsCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return DataTable
		public DataTable SelectByCreateDateRDT_Paged(DateTime CreateDate, int PageNumber, int PageSize)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _ProductsCollection = new EntityCollection(new ProductsEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(ProductsFields.CreateDate == CreateDate);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_ProductsCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, 0, null, true, null, PageNumber, PageSize);
			}
			return toReturn;
		}


		
		

	}
}
